<?php

namespace backend\modules\supplier\models;

use Yii;

/**
 * 这个模型基于表 "{{%s_storage}}". 生成
 *
 * @property integer $id
 * @property integer $supplier_id
 * @property string $name
 * @property string $address
 * @property string $linkman
 * @property string $tel
 * @property string $remark
 */
class Storage extends \common\models\AR {

    public $modelName = '仓库';

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%s_storage}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['supplier_id', 'name'], 'required'],
            [['supplier_id'], 'integer'],
            [['remark'], 'string'],
            [['name', 'linkman'], 'string', 'max' => 50],
            [['address'], 'string', 'max' => 200],
            [['tel'], 'string', 'max' => 30]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'supplier_id' => '所属供应商',
            'name' => '名字',
            'address' => '地址',
            'linkman' => '联系人',
            'tel' => '电话',
            'remark' => '备注',
        ];
    }

    public function beforeValidate() {
        if (parent::beforeValidate()) {
            $this->supplier_id = Yii::$app->user->identity->supplier_id;
            return true;
        } else {
            return FALSE;
        }
    }
   
    
    /**
     * 获取仓库被引用的次数
     * @return integer 货位被引用的次数
     */
    public function getUsedAcount() {
        return $this->hasMany(Rack::className(), ['storage_id' => 'id'])->count();
    }

    /**
     * 获取当前供应商的所有仓库列表
     * @return type
     */
    public static function getAll() {
        return self::find()->where('supplier_id=:supplier_id', [':supplier_id' => Yii::$app->user->identity->supplier_id])->all();
    }
    


}
